//
//  DDBossDiscoverViewController.m
//  DingDongZP
//
//  Created by Jyh on 16/4/20.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDBossDiscoverViewController.h"
#import "DDJobfinderDetailViewController.h"
#import "DDBossPublishJobViewController.h"
#import "DDJobfindersService.h"
#import "DDBossJobService.h"
#import "DDDiscoverJobFinderCell.h"
#import "DDBossPublishedJobModel.h"
#import "HMSegmentedControl.h"
#import "SDCycleScrollView.h"
#import "BossHomePullView.h"
#import "DDSegementTabbarView.h"
#import "DDCommonDataModel.h"
#import <MJRefresh/MJRefresh.h>

#define kTableViewHeight    kScreenHeight  - 49 -64 -44

static NSString *DDDiscoverJobfinderCellIdentifier = @"DDDiscoverJobFinderCell";

@interface DDBossDiscoverViewController () <SDCycleScrollViewDelegate, UIActionSheetDelegate>

typedef enum TableShowCommType{
    
    ShowNoLogin,//触发逛一逛
    ShowNoPublishJob,//没有发布职位
    ShowPublishedJob//有发布职位
    
}TableShowType;

typedef enum RequestNetType{
    
    New = 0,
    More = 1
}RequestNetType;


//type
@property (assign, nonatomic) TableShowType tabbleType;
@property (assign, nonatomic) RequestNetType requestType;


@property (strong, nonatomic) HMSegmentedControl *segmentedControl;

//@property (strong, nonatomic) SDCycleScrollView *cycleScrollView;

@property (strong, nonatomic) DDJobfindersService *jobfindersService;

@property (strong, nonatomic) DDBossJobService *jobService;

@property (strong, nonatomic) UIButton *showMenuButton;

// 设置默认职位ID
@property (copy, nonatomic) NSString *expectJobId;

@property (strong, nonatomic) BossHomePullView *titlePullView;
@property (strong, nonatomic) NSString *titleString;
@property (strong, nonatomic) UILabel *titleTouchLabel;

@property (strong, nonatomic) NSMutableArray *pulishedJobArray;

// 月薪 经验 学历View
@property (strong, nonatomic) DDSegementTabbarView *segmentView;

@property (strong, nonatomic) NSMutableArray *eduArray;
@property (strong, nonatomic) NSMutableArray *experiencesArray;
@property (strong, nonatomic) NSMutableArray *salaryArray;
// 筛选类型
@property (strong, nonatomic) NSString *filterType;
// 筛选条件ID
@property (strong, nonatomic) NSString *filterId;
//判断数据
@property (strong, nonatomic) NSMutableArray *recommendArray;
@property (strong, nonatomic) NSMutableArray *hottestArray;
@property (strong, nonatomic) NSMutableArray *lastestArray;
//判断page
@property (assign, nonatomic) int recommendCurrentPage;
@property (assign, nonatomic) int hottestCurrentPage;
@property (assign, nonatomic) int lastestCurrentPage;
@property (assign, nonatomic) int filterCurrentPage;
//
@property (assign, nonatomic) UITableView *currentTableView;
@property (strong, nonatomic) UIImageView *adImageView;

@property (nonatomic, strong) DDBossInfoService *bossService;

@end

@implementation DDBossDiscoverViewController

#pragma mark
#pragma mark - View LifeCycle


- (instancetype)init
{
    self = [super init];
    if (self) {
        self.jobfindersService = [[DDJobfindersService alloc] init];
        self.jobService = [[DDBossJobService alloc] init];
    }
    return self;
}

- (DDBossInfoService *)bossService
{
    if (!_bossService){
        _bossService = [[DDBossInfoService alloc] init];
    }
    return _bossService;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupView];
    [self setData];
    /**
     *  收到 alert dissmiss的通知   以改变图标
     *
     */
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeImage) name:@"dissmissAlertVC" object:nil];
    
    // 保存Boss 端userid
    [self.bossService queryBossInfoWithSuccessBlock:^(id responseObject) {
        DDBossInfo *model = [DDBossInfo yy_modelWithJSON:responseObject[@"data"]];
        [DDLoginUserManager setWeixin:model.weixin];
        [DDLoginUserManager setUserID:model.userid];
    } failBlock:nil];

}


- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
//    [self getMyPublishedJobs];
    [self.rdv_tabBarController setTabBarHidden:false animated:true];
    
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:false];
    
    if (!self.titlePullView.hidden && self.titlePullView.isShowPullView) {
        [self.titlePullView dismissPullView];
        self.titleTouchLabel.attributedText = [self setTitleAttributed];
    }
}

- (void)changeImage
{
    WEAKSELF
    if (weakSelf.segmentView.salaryButton.selected == YES) {
        weakSelf.segmentView.salaryButton.selected = NO;
    }
    if ( weakSelf.segmentView.experienceButton.selected == YES) {
        weakSelf.segmentView.experienceButton.selected = NO;
    }
    if (weakSelf.segmentView.eduButton.selected == YES) {
       
        weakSelf.segmentView.eduButton.selected = NO;
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark
#pragma mark - Private Methods
//初始化对象
- (void)setData
{
    self.pulishedJobArray = [[NSMutableArray alloc] init];
    
    self.recommendArray = [[NSMutableArray alloc] init];
    self.hottestArray = [[NSMutableArray alloc] init];
    self.lastestArray = [[NSMutableArray alloc] init];
    
    _recommendCurrentPage = 1;
    _hottestCurrentPage = 1;
    _lastestCurrentPage = 1;
    _filterCurrentPage = 1;
    
    [self getMyPublishedJobs];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getMyPublishedJobs) name:NOTI_BOSS_EDIT_JOBINFO object:nil];
}

- (void)setupView
{
    [self.view addSubview:self.showMenuButton];
    // segmentControl
    [self.view addSubview:self.segmentedControl];
    self.view.backgroundColor = COLOR_BACKGROUND_GRAY;
    
    // 设置更多选项View
    [self setSegementView];
    [self addRightButtonItemWithImage:[UIImage imageNamed:@"common_nav_search"] action:@selector(searchClick)];
    self.tableViewRecommend.frame = CGRectMake(0, 0, kScreenWidth,kTableViewHeight);
    self.tableViewRecommend.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //  去掉发布职位的界面
//    [self.view addSubview:self.noJobView];
//    self.noJobView.center = self.view.center;
//    [self.view bringSubviewToFront:self.noJobView];
    
    [self setUpBossADScrollView];
}

//搜索事件
- (void)searchClick
{
    self.hidesBottomBarWhenPushed = true;
    LDFinderSearchVC *searchVC = [[LDFinderSearchVC alloc] init];
    [self.navigationController pushViewController:searchVC animated:false];
}
//分栏
- (void)setSegementView
{
    self.segmentView = [DDSegementTabbarView segementView];
//    self.segmentView.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, 40);
    
    [self.view addSubview:self.segmentView];
    
    [self.segmentView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(kScreenWidth);
        make.height.mas_equalTo(40);
        make.left.mas_equalTo(kScreenWidth);
        
    }];
    
    WEAKSELF
    
    [self.segmentView.hideButton bk_addEventHandler:^(id sender) {
        [UIView animateWithDuration:0.3f animations:^{
            weakSelf.segmentView.frame = CGRectMake(kScreenWidth, 0, kScreenWidth, 40);
        }];
    } forControlEvents:UIControlEventTouchUpInside];
    
    // 月薪
    [self.segmentView.salaryButton bk_addEventHandler:^(id sender) {
        
        weakSelf.segmentView.salaryButton.selected = YES;
        weakSelf.segmentView.experienceButton.selected = NO;
        weakSelf.segmentView.eduButton.selected = NO;
        
        [weakSelf showSalaryChoose];
        
    } forControlEvents:UIControlEventTouchUpInside];
    
    // 经验
    [self.segmentView.experienceButton bk_addEventHandler:^(id sender) {
        
        weakSelf.segmentView.experienceButton.selected = YES;
        weakSelf.segmentView.salaryButton.selected = NO;
        weakSelf.segmentView.eduButton.selected = NO;
        
        [weakSelf showExperienceChoose];
        
    } forControlEvents:UIControlEventTouchUpInside];
    
    // 规模
    [self.segmentView.eduButton bk_addEventHandler:^(id sender) {
        
        weakSelf.segmentView.eduButton.selected = YES;
        weakSelf.segmentView.salaryButton.selected = NO;
        weakSelf.segmentView.experienceButton.selected = NO;
        
        [weakSelf showEduChoose];
        
    } forControlEvents:UIControlEventTouchUpInside];
}

- (void)showSalaryChoose
{
    WEAKSELF
    
    [[LDCheckPublicInfo shareInstance] checkoutAllSalaryJobfinder:^(NSArray<NSDictionary *> * _Nonnull data) {
        
        NSArray *titleArr = [self getArrFormDictArrFilterKey:@"salary_text" arrDict:data];
        NSArray *titleArrID = [self getArrFormDictArrFilterKey:@"salary_id" arrDict:data];
        
        [self showLDActionSheetView:@"薪资" labHeight:nil labsTitleArr:titleArr selClosure:^(NSInteger index) {
            
            weakSelf.segmentView.salaryButton.selected = NO;
            weakSelf.filterId = titleArrID[index];
            weakSelf.filterType = @"salary";
            [weakSelf requestSiftJobfinder:New];
            
        }];
    }];
}

- (void)showExperienceChoose
{
    WEAKSELF
    
    [[LDCheckPublicInfo shareInstance] checkoutAllExperienceJobfinder:^(NSArray<NSDictionary *> * _Nonnull data) {
        
        NSArray *titleArr = [self getArrFormDictArrFilterKey:@"experience_text" arrDict:data];
        NSArray *titleArrID = [self getArrFormDictArrFilterKey:@"experience_id" arrDict:data];
        
        [self showLDActionSheetView:@"经验" labHeight:nil labsTitleArr:titleArr selClosure:^(NSInteger index) {
            
            weakSelf.segmentView.experienceButton.selected = NO;
            weakSelf.filterId = titleArrID[index];
            weakSelf.filterType = @"experience";
            [weakSelf requestSiftJobfinder:New];
            
        }];
    }];
}

- (void)showEduChoose
{
    WEAKSELF
    [[LDCheckPublicInfo shareInstance] checkoutAllMainEduInfoJobfinder:^(NSArray<NSDictionary *> * _Nonnull data) {
        
        NSArray *titleArr = [self getArrFormDictArrFilterKey:@"edu_text" arrDict:data];
        NSArray *titleArrID = [self getArrFormDictArrFilterKey:@"edu_id" arrDict:data];
        
        [self showLDActionSheetView:@"学历" labHeight:nil labsTitleArr:titleArr selClosure:^(NSInteger index) {
            
            weakSelf.segmentView.eduButton.selected = NO;
            weakSelf.filterId = titleArrID[index];
            weakSelf.filterType = @"edu";
            [weakSelf requestSiftJobfinder:New];
            
        }];
    }];
}


- (void)setCurrentJobId
{
    WEAKSELF
    [self.jobService setMyselfCurrentJobId:self.expectJobId SuccessBlock:^(id responseObject) {
        if ([weakSelf isValidateResponseData:responseObject]) {
            
            NSDictionary *dic= @{@"current_jobid":weakSelf.expectJobId};
            [[NSNotificationCenter defaultCenter] postNotificationName:NOTI_EMPLOYER_SET_CURRENTJOB object:weakSelf userInfo:dic];
            
            [weakSelf.recommendArray removeAllObjects];
            [weakSelf requestRecommendJobfindersData:New];
        } else {
            
            [weakSelf responseUnValidate:responseObject];
        }
        
    } failBlock:^(id responseObject, NSError *error) {
        //错误提示统一处理
        //        [weakSelf requestFaild];
    }];
}

- (void)setNoDataViewHidden:(BOOL)hidden
{
    if (hidden) {
        
//        self.noJobView.hidden = YES;
        
        for (id view in self.scrollView.subviews) {
            if ([view isKindOfClass:[UITableView class]]) {
                UITableView *table = (UITableView *)view;
                table.hidden = false;
            }
        }
        
        
    } else {
        
        for (id view in self.scrollView.subviews) {
            if ([view isKindOfClass:[UITableView class]]) {
                UITableView *table = (UITableView *)view;
                table.hidden = YES;
            }
        }
//        self.noJobView.hidden = false;
        
    }
}

/**
 *  设置广告轮播图
 */
- (void)setUpBossADScrollView
{

    UIView *headerView = [[UIView alloc] init];
    headerView.frame = CGRectMake(0, 0, kScreenWidth, kScreenWidth * kDiscoverAdsScale);
    headerView.backgroundColor = COLOR_BACKGROUND;
    
    
    _adImageView = [[UIImageView alloc] init];
    _adImageView.layer.cornerRadius = 3;
    _adImageView.layer.masksToBounds = true;
    
    [headerView addSubview:_adImageView];
    
    self.tableViewRecommend.tableHeaderView = headerView;
    [self.adImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(headerView).with.insets(UIEdgeInsetsMake(8, 10, 0, 10));
    }];
    
    //加入缓存机制
    WEAKSELF
    [[LDBossInfoNet sharedInstance] queryEmployerAds:^(NSArray<NSString *> * _Nonnull dictArr) {
        
        NSURL *url = [NSURL URLWithString:dictArr[0]];
        [weakSelf.adImageView sd_setImageWithURL:url];
        
    } failure:^(id  _Nonnull error) {
        
        
    }];
}

#pragma mark
#pragma mark - TitleView

- (void)setNavgationBarTitleWithInfo:(NSArray *)info
{
    
    self.pulishedJobArray = [NSMutableArray arrayWithArray:info];
    
    NSMutableArray *jobTitleArr = [NSMutableArray array];
    NSMutableArray *jobTitle_IDArr = [NSMutableArray array];
    
    
    for (NSDictionary *dict in info) {
        
        [jobTitleArr addObject:dict[@"jobtitle"]];
        [jobTitle_IDArr addObject:dict[@"jobid"]];
        
    };
    
    //默认刷新第一个
    [jobTitleArr addObject:@"逛一逛"];
    [self updateWithTypeIdx:0];
    
    self.titleString = jobTitleArr[0];
    
    self.titlePullView = [[BossHomePullView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 -49)];
    self.titlePullView.hidden = YES;
    [self.view addSubview:_titlePullView];
    
    self.titleTouchLabel = [[UILabel alloc] init];
    self.titleTouchLabel.textAlignment = NSTextAlignmentCenter;
    
    self.titleTouchLabel.attributedText = [self setTitleAttributed];
    self.titleTouchLabel.frame = CGRectMake(0, 0, 180, 30);
    self.navigationItem.titleView = self.titleTouchLabel;
    
    WEAKSELF
    
    self.titlePullView.titles = jobTitleArr;
    self.titlePullView.seletedTitleBlock = ^(NSInteger idx) {
        
        weakSelf.titleString = jobTitleArr[idx];
        weakSelf.titleTouchLabel.attributedText = [weakSelf setTitleAttributed];
        [weakSelf updateWithTypeIdx:idx];
    };
    
    self.titleTouchLabel.userInteractionEnabled = YES;
    [self.titleTouchLabel bk_whenTapped:^{
        
        if (![weakSelf.titlePullView isShowPullView]) {
            
            [weakSelf.titlePullView showPullView];
        } else {
            
            [weakSelf.titlePullView dismissPullView];
        }
        weakSelf.titleTouchLabel.attributedText = [weakSelf setTitleAttributed];
    }];

}
//
- (void)updateWithTypeIdx:(NSInteger)idx
{
    if (idx == [self.pulishedJobArray count]) {
        
        self.tabbleType = ShowNoLogin;
        [self justSeeLittle];
        
    } else {
        
        self.tabbleType = ShowPublishedJob;
        NSString *jobid = self.pulishedJobArray[idx][@"jobid"];
        
        self.segmentedControl.hidden = false;
        self.showMenuButton.hidden = false;
        
        self.scrollView.frame = CGRectMake(0, 44, kScreenWidth, kTableViewHeight);
        self.tableViewRecommend.frame = CGRectMake(0, 0, kScreenWidth, kTableViewHeight);
        
        self.scrollView.scrollEnabled = true;
        [self setNoDataViewHidden:YES];
        self.expectJobId = jobid;
        [self setCurrentJobId];
    }
}

// 逛一逛
-(void)justSeeLittle {

    WEAKSELF
    [self requestJobsData:^(NSArray *dictArr) {
        
        weakSelf.segmentedControl.hidden = true;
        weakSelf.showMenuButton.hidden = true;
        [weakSelf.recommendArray removeAllObjects];
        
        weakSelf.scrollView.frame = CGRectMake(0, 0, kScreenWidth, kTableViewHeight + 44);
        weakSelf.scrollView.scrollEnabled = false;
        weakSelf.tableViewRecommend.frame = self.scrollView.frame;
        
        [weakSelf.recommendArray addObjectsFromArray:dictArr];
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
           [weakSelf.tableViewRecommend reloadData];
        }];
        
        
        [weakSelf hideNoDataView];
        [self setNoDataViewHidden:YES];
        
        [weakSelf endRefreshHeader];
    } failure:^(id error) {
        
        [weakSelf endRefreshHeader];
        [[LDShowHUB shareInstance] showBarStatus:error];
    }];
    
}

- (NSMutableAttributedString *)setTitleAttributed
{
    NSMutableAttributedString *titleAttributed = [[NSMutableAttributedString alloc] initWithString:self.titleString];
    [titleAttributed addAttribute:NSForegroundColorAttributeName
                            value:[UIColor whiteColor]
                            range:NSMakeRange(0,self.titleString.length)];
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    
    if ([self.titlePullView isShowPullView]) {
        
        textAttachment.image = [UIImage imageNamed:@"pull_up"];
    } else {
        
        textAttachment.image = [UIImage imageNamed:@"pull_down"];
    }
    
    textAttachment.bounds = CGRectMake(5, 2, 9, 5);
    [titleAttributed insertAttributedString:[NSAttributedString attributedStringWithAttachment:textAttachment] atIndex:self.titleString.length];
    return titleAttributed;
}

#pragma mark
#pragma mark - Event Response

- (void)showMenuButtonClick:(UIButton *)button
{
    [UIView animateWithDuration:0.3f animations:^{
        
        self.segmentView.frame = CGRectMake(0, 0, kScreenWidth, 40);
    }];
}

- (void)bossOnPublishClick:(UIButton *)button
{
    DDBossPublishJobViewController *pulishJobVC = [[DDBossPublishJobViewController alloc] init];
    pulishJobVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:pulishJobVC animated:YES];
}

#pragma mark
#pragma mark - NetWork

/** 获取我发布的职位 */
- (void)getMyPublishedJobs {
    WEAKSELF
    
    [[LDBossInfoNet sharedInstance] viewMyselfAllJobsKinds:^(NSArray<NSDictionary *> * _Nonnull dictArr) {
    
        if (dictArr.count == 0) {
            
            [weakSelf setNoDataViewHidden:NO];
            weakSelf.showMenuButton.hidden = YES;
            weakSelf.segmentedControl.hidden = YES;
            
        } else {
            
            weakSelf.segmentedControl.hidden = NO;
            weakSelf.showMenuButton.hidden = NO;
            [weakSelf setNoDataViewHidden:YES];
        }
        
        NSMutableArray *mutarr = [NSMutableArray array];
        
        for (NSDictionary *dict in dictArr) {
            
            NSString *isHidden = dict[@"ishidden"];
            
            if (![isHidden isEqualToString:@"1"]) {
                
                [mutarr addObject: dict];
                
            }
        }
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
           
            [weakSelf setNavgationBarTitleWithInfo:mutarr];
        }];
        
    } failure:^(id  _Nonnull error) {
        
    }];
}

//加载新数据

-(void)loadNewData:(UITableView *)tableview {

    if (tableview == self.tableViewRecommend) {
        
        if (self.filterType != nil) {
            
            [self requestSiftJobfinder:New];
            
        } else if (self.tabbleType == ShowNoLogin) {
            //刷新逛一逛
            [self justSeeLittle];
            
        } else {
            
            [self requestRecommendJobfindersData:New];
        }
        
    } else if (tableview == self.tableViewHotest) {
        
        
        [self requestHottestJobfidersData:New];
    } else if (tableview == self.tableViewLatest) {
        
        [self requestLatestJobfidersData:New];
    }
}

//获取更多数据
-(void) loadMoreData:(UITableView *)tableview{
    
    if (tableview == self.tableViewRecommend) {
        
        if (self.filterType != nil) {
            
            [self requestSiftJobfinder:More];
            
        } else if (self.tabbleType == ShowNoLogin) {
            //提示弹窗
            [self popRemaind];
            
        } else {
            
            [self requestRecommendJobfindersData:More];
        }
        
        
    } else if (tableview == self.tableViewHotest) {
        
        [self requestHottestJobfidersData:More];
    } else if (tableview == self.tableViewLatest) {
        
        [self requestLatestJobfidersData:More];
    }
}


- (void)requestRecommendJobfindersData:(RequestNetType)type {
    
    self.filterType = nil;//清空选择栏标记
    
    int page = 0;
    
    if (type == New) {
        page = 0;
        
    } else {
        page = _recommendCurrentPage;
    }
    
    NSString *currentpage = [[NSString alloc] initWithFormat:@"%d",page];
    
    WEAKSELF
    [self.jobService queryRecommendJobfindersWithJobId:self.expectJobId page:currentpage SuccessBlock:^(id responseObject) {
        if ([weakSelf isValidateResponseData:responseObject]) {
            
            // 请求成功
            NSDictionary *dictInfo = responseObject[@"data"];
            NSArray *arrInfo = dictInfo[@"jobfinders"];
            NSString *hasNextPage = [[NSString alloc] initWithFormat:@"%@",dictInfo[@"hasNextPage"]];
            
            //判断逻辑只在刷新时候判断
            if ([arrInfo count] == 0  && (page == 0)) {
                
                [weakSelf showNoDataViewWithImage:nil text:@"暂无数据"];
            } else {
                [weakSelf hideNoDataView];
            }
            
            //根据page大小正反向添加数组
            if (type == New) {
               
                for (NSDictionary *item in arrInfo) {
                    
                    if ([weakSelf.recommendArray containsObject:item] == false) {
                        
                        [weakSelf.recommendArray insertObject:item atIndex:0];
                    }
                }
                
            } else if([hasNextPage isEqualToString:@"0"]) {
                
                
                for (NSDictionary *item in arrInfo) {
                    
                    if ([weakSelf.recommendArray containsObject:item] == false) {
                        
                        [weakSelf.recommendArray addObjectsFromArray:arrInfo];
                    }
                }
               
            } else {//if(hasNextPage == 0) {
                
                [weakSelf.recommendArray addObjectsFromArray:arrInfo];
                weakSelf.recommendCurrentPage =+ 1;
                [weakSelf.tableViewRecommend.mj_footer endRefreshingWithNoMoreData];
            }
        
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                
                [weakSelf.tableViewRecommend reloadData];
            }];
            
            
        } else {
            //参数错误
        }
        
        [weakSelf endRefreshHeader];
        [weakSelf endRefreshFooter];
        
    } failBlock:^(id responseObject, NSError *error) {
        
        [weakSelf endRefreshHeader];
        [weakSelf endRefreshFooter];
        [weakSelf requestFaild];
        
    }];
}
- (void)requestHottestJobfidersData:(RequestNetType)type
{
    [self requestJobfindersData:@"hottest" type:type];
}

- (void)requestLatestJobfidersData:(RequestNetType)type
{
    [self requestJobfindersData:@"latest" type:type];
}
//
- (void)requestJobfindersData:(NSString *)order type:(RequestNetType)type
{
    
    self.filterType = nil;//清空选择栏标记
    
    int page = 0;
    
    if (type == New) {
        page = 0;
        
    } else {
        
        if ([order isEqualToString:@"hottest"]) {
            
            page = _hottestCurrentPage;
        } else {
            page = _lastestCurrentPage;
        }
    }
    NSString *currentpage = [[NSString alloc] initWithFormat:@"%d",page];
    
    WEAKSELF
    [self.jobService queryJobfindersWithJobId:self.expectJobId order:order page:currentpage SuccessBlock:^(id responseObject) {
        
        if ([weakSelf isValidateResponseData:responseObject]) {
            // 请求成功
            NSDictionary *dictInfo = responseObject[@"data"];
            NSArray *arrInfo = dictInfo[@"jobfinders"];
            NSString *hasNextPage = [[NSString alloc] initWithFormat:@"%@",dictInfo[@"hasNextPage"]];
            
            if ([arrInfo count] == 0 && page == 0) {
                
                [weakSelf showNoDataViewWithImage:nil text:@"暂无数据"];
            } else {
                
                [weakSelf hideNoDataView];
            }
            
            
            if ([order isEqualToString:@"hottest"]) {
                
                if (type == New) {
                    
                    for (NSDictionary *item in arrInfo) {
                        
                        if ([weakSelf.hottestArray containsObject:item] == false) {
                            
                            [weakSelf.hottestArray insertObject:item atIndex:0];
                        }
                    }
                } else {
                    
                    if ([hasNextPage isEqualToString:@"1"]) {
                        
                        _hottestCurrentPage += 1;
                        [weakSelf.hottestArray addObjectsFromArray:arrInfo];
                        
                    } else {
                        
                        for (NSDictionary *item in arrInfo) {
                            
                            if ([weakSelf.hottestArray containsObject:item] == false) {
                                
                                [weakSelf.hottestArray addObjectsFromArray:arrInfo];
                            }
                        }
                    }
                    
                }
            
                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                    
                    [weakSelf.tableViewHotest reloadData];
                }];
                
            }
            
            
            if ([order isEqualToString:@"latest"]) {
                
                
                if (type == New) {
                    
                    for (NSDictionary *item in arrInfo) {
                        
                        if ([weakSelf.lastestArray containsObject:item] == false) {
                            
                            [weakSelf.lastestArray insertObject:item atIndex:0];
                        }
                    }
                } else {
                    
                    if ([hasNextPage isEqualToString:@"1"]) {
                        
                        _lastestCurrentPage += 1;
                        [weakSelf.lastestArray addObjectsFromArray:arrInfo];
                        
                    } else {
                        
                        for (NSDictionary *item in arrInfo) {
                            
                            if ([weakSelf.lastestArray containsObject:item] == false) {
                                
                                [weakSelf.lastestArray addObjectsFromArray:arrInfo];
                            }
                        }
                    }
                    
                }
            }
            
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                
                [weakSelf.tableViewLatest reloadData];
            }];
            
        } else {
            //参数等错误处理
        }
        
        [weakSelf endRefreshHeader];
        [weakSelf endRefreshFooter];
        
    } failBlock:^(id responseObject, NSError *error) {
        
        [weakSelf endRefreshHeader];
        [weakSelf endRefreshFooter];
        [weakSelf requestFaild];
    }];
    
}

/** 请求筛选条件的数据 */
- (void)requestSiftJobfinder:(RequestNetType ) type
{
    WEAKSELF
    int page = 0;
    
    if (type == New) {
        page = 0;
        
    } else {
        page = _filterCurrentPage;
    }
    
    NSString *currentpage = [[NSString alloc] initWithFormat:@"%d",page];
    [self.jobService queryJobfindersWithJobId:self.expectJobId Filtertype:self.filterType FilterID:self.filterId page:currentpage SuccessBlock:^(id responseObject) {
        
        //筛选时清空处理
        if ([weakSelf isValidateResponseData:responseObject]) {
            
            // 请求成功
            
            //成功后切换 主视图
            [weakSelf.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
            
            NSDictionary *dictInfo = responseObject[@"data"];
            NSArray *arrInfo = dictInfo[@"jobfinders"];
            NSString *hasNextPage = [[NSString alloc] initWithFormat:@"%@",dictInfo[@"hasNextPage"]] ;
            
            //根据page大小正反向添加数组
            if ([currentpage isEqualToString:@"0"]) {
                
                if ([arrInfo count] == 0) {
                    
                    [weakSelf showNoDataViewWithImage:nil text:@"暂无数据"];
                } else {
                    
                    [weakSelf hideNoDataView];
                }
                
                //逻辑暂时简化处理
                [weakSelf.recommendArray removeAllObjects];
                [weakSelf.recommendArray addObjectsFromArray:arrInfo];
                
//                for (NSDictionary *item in arrInfo) {
//                    
//                    if ([weakSelf.recommendArray containsObject:item] == false) {
//                        
//                        [weakSelf.recommendArray insertObject:item atIndex:0];
//                    }
//                }
                
            } else if([hasNextPage isEqualToString:@"1"]) {
                
                [weakSelf.recommendArray addObjectsFromArray:arrInfo];
                weakSelf.filterCurrentPage =+ 1;
                
            } else {//if([hasNextPage isEqualToString:@"0"])
                //
                [weakSelf.tableViewRecommend.mj_footer endRefreshingWithNoMoreData];
            }
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
                [weakSelf.tableViewRecommend reloadData];
            }];
            
        } else {

        }
        [weakSelf endRefreshHeader];
        [weakSelf endRefreshFooter];
    } failBlock:^(id responseObject, NSError *error) {
        [weakSelf requestFaild];
    }];
}
/**  未登录状态最多能展示15条， index从0开始 */
- (void)requestJobsData:(void (^)(NSArray *dictArr))success failure:(void (^)(id error))failure
{
    WEAKSELF
    [self.jobfindersService queryJobfindersWithPage:@"0" successBlock:^(id responseObject) {
        [weakSelf hideLoading];
        
        if ([weakSelf isValidateResponseData:responseObject]) {
           // 请求成功
            
            
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                
                // 请求成功
                NSDictionary *dictInfo = responseObject[@"data"];
                NSArray *arrInfo = dictInfo[@"jobfinders"];
                
                if ([arrInfo count] == 0) {
                    
                    [weakSelf showNoDataViewWithImage:nil text:@"暂无数据"];
                } else {
                    [weakSelf hideNoDataView];
                }
                
                success(arrInfo);

            }];
            
        
        } else {
            failure(@"操作失败");
        }
        
    } failBlock:^(id responseObject, NSError *error) {
        
        failure(@"网络错误");
    }];
}

#pragma mark -
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableViewRecommend) {
        
        return [self.recommendArray count];
        
    } else if (tableView == self.tableViewHotest) {
        
        return [self.hottestArray count];
    } else if (tableView == self.tableViewLatest) {
        
        return [self.lastestArray count];
    } else {
        
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [self getCellFromTypeCategory:tableView indexPath:indexPath];
}
//返回cell
-(LDBossRecJobFinderCell *) getCellFromTypeCategory:(UITableView *) tableView indexPath:(NSIndexPath *)indexPath {
    
    LDBossRecJobFinderCell *cell;
    cell = (LDBossRecJobFinderCell *)[tableView dequeueReusableCellWithIdentifier:@"DDBossDiscoverCellIdentifier"];
    
    if (cell == nil) {
        
        cell = [[LDBossRecJobFinderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DDBossDiscoverCellIdentifier"];
    }
    
    if (tableView == self.tableViewRecommend) {
        
        DDJobfinderDetail *models = [DDJobfinderDetail yy_modelWithDictionary:self.recommendArray[indexPath.row]];
        cell.models = models;
        
    } else if (tableView == self.tableViewHotest) {
        
        DDJobfinderDetail *models = [DDJobfinderDetail yy_modelWithDictionary:self.hottestArray[indexPath.row]];
        cell.models = models;
        
    } else if (tableView == self.tableViewLatest) {
        
        DDJobfinderDetail *models = [DDJobfinderDetail yy_modelWithDictionary:self.lastestArray[indexPath.row]];
        cell.models = models;
        
    } 
    
    return cell;
}

#pragma mark -
#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    DDJobfinderDetail *jobfinderDetail;
    
    if (tableView == self.tableViewRecommend) {
        
        jobfinderDetail = [DDJobfinderDetail yy_modelWithDictionary:self.recommendArray[indexPath.row]];
    } else if (tableView == self.tableViewHotest) {
        
        jobfinderDetail = [DDJobfinderDetail yy_modelWithDictionary:self.hottestArray[indexPath.row]];
    } else {
        
        jobfinderDetail = [DDJobfinderDetail yy_modelWithDictionary:self.lastestArray[indexPath.row]];
    }
    
    if (self.pulishedJobArray.count < 1) {
        
//        WEAKSELF
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
        
            [self showLDAlertView:@"没有发布职位，是否前往" sure:^{
                
//                DDBossPublishedJobViewController *vc = [[DDBossPublishedJobViewController alloc] init];
//                [weakSelf.navigationController pushViewController:vc animated:true];
            UITabBarController *tabVC = (UITabBarController *)[[[UIApplication sharedApplication] keyWindow ] rootViewController];
            RDVTabBarController *rootvc = (LDMainTabBarVC *)tabVC;
            rootvc.selectedIndex = 3;
                
            } cancel:nil];
        }];
        
    } else {
        
        LDJobfinderDetailBaseVCInAll_Show *vc = [[LDJobfinderDetailBaseVCInAll_Show alloc] init];
        vc.userID = jobfinderDetail.userid;
//        vc.model = jobfinderDetail;
//        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        
    }
}

#pragma mark
#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    if (![scrollView isKindOfClass:[UITableView class]]) {
        NSInteger page = (scrollView.contentOffset.x + scrollView.frame.size.width/2)/ scrollView.frame.size.width;
        
        [self.segmentedControl setSelectedSegmentIndex:page animated:YES];
        [self didSelectIndexOfSegment:page];
    }
}

#pragma mark
#pragma mark - HMSegmentedControlDelegate

- (void)bossdiscoverSegmentedControlChangedValue:(HMSegmentedControl *)segement
{
    [self didSelectIndexOfSegment:(NSInteger)(segement.selectedSegmentIndex)];
}

-(void) didSelectIndexOfSegment:(NSInteger)index {
    
    if (index == 0) {
        // 推荐
        [self requestRecommendJobfindersData:0];
    } else if (index ==1) {
        // 最热
        [self requestHottestJobfidersData:New];
    } else {
        // 最新
        [self requestLatestJobfidersData:New];
    }
}
#pragma mark
#pragma mark - Getters and Setters

- (HMSegmentedControl *)segmentedControl
{
    if (!_segmentedControl) {
        _segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"推荐", @"最热", @"最新"]];
        _segmentedControl.backgroundColor = [UIColor whiteColor];
        _segmentedControl.titleTextAttributes = @{NSForegroundColorAttributeName:COLOR_FONT_DARK_GRAY,NSFontAttributeName:[UIFont systemFontOfSize:15]};
        _segmentedControl.selectionIndicatorHeight = 2.f;
        _segmentedControl.selectionIndicatorColor = COLOR_NAVIGATIONBAR;
        _segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
        _segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
        WEAKSELF
        [_segmentedControl setIndexChangeBlock:^(NSInteger index) {
            [weakSelf.scrollView setContentOffset:CGPointMake(index * SCREEN_WIDTH, 0) animated:YES];
        }];
        _segmentedControl.layer.shadowColor = COLOR_BORDER_GRAY.CGColor;
        _segmentedControl.layer.shadowOffset = CGSizeMake(0.0f, 3.0f);
        _segmentedControl.frame = CGRectMake(0, 0, SCREEN_WIDTH - 50, 40);
        [_segmentedControl addTarget:self action:@selector(bossdiscoverSegmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
    }
    return _segmentedControl;
}

//- (SDCycleScrollView *)cycleScrollView
//{
//    if (!_cycleScrollView) {
//        _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 66) delegate:self placeholderImage:[UIImage imageNamed:@""]];
//        _cycleScrollView.layer.cornerRadius = 5;
//        _cycleScrollView.layer.masksToBounds = YES;
//        _cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
//        
//        _cycleScrollView.currentPageDotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
//    }
//    
//    return _cycleScrollView;
//}

- (UIButton *)showMenuButton
{
    if (!_showMenuButton) {
        _showMenuButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_showMenuButton setImage:[UIImage imageNamed:@"button_job_switch"] forState:UIControlStateNormal];
        _showMenuButton.backgroundColor = [UIColor whiteColor];
        _showMenuButton.frame = CGRectMake(SCREEN_WIDTH - 50, 0, 50, 40);
        _showMenuButton.layer.borderColor = COLOR_BORDER_LIGHT_GRAY.CGColor;
        _showMenuButton.layer.borderWidth = 0.5;
        [_showMenuButton addTarget:self action:@selector(showMenuButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _showMenuButton;
}

// 逛一逛提示弹窗
- (void)popRemaind
{
    [self endRefreshFooter];
    
//    WEAKSELF
    if (self.pulishedJobArray.count < 1) {
        
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            
            [self showLDAlertView:@"没有发布职位，是否前往" sure:^{
                
//                DDBossPublishedJobViewController *vc = [[DDBossPublishedJobViewController alloc] init];
//                [weakSelf.navigationController pushViewController:vc animated:true];
            UITabBarController *tabVC = (UITabBarController *)[[[UIApplication sharedApplication] keyWindow ] rootViewController];
            RDVTabBarController *rootvc = (LDMainTabBarVC *)tabVC;
            rootvc.selectedIndex = 3;
                
            } cancel:nil];
        }];
    }else{
    
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            
            [self showLDAlertView:@"逛一逛最多显示20条数据"];
        }];
    }
}

@end
